<template>
  <div style="width: 100%">
    <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @save="save" ></v-tableTitle>
    <div class="detailBox" style="height:300px">
      <v-detail ref="detail"></v-detail>
    </div>
    <div class="detailListBox">
      <v-detailList ref="detailList" :apiParams="apiParams" @handleSave="save"></v-detailList>
    </div>
  </div>
</template>

<script>
import baseMixin from "@/mixins/BaseMixin";

import tableTitle from "commons/TableTitle";
import detail from "../details/Detail";
import detailList from "../detailsList/DetailList";

export default {
  name: "dispatchDetail",
  mixins: [baseMixin],
  data() {
    return {
      apiParams:{deliveryId:-1},
      updateKey:null
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.save'), certigier: !this.btnPermNameList.includes('WM06070104'),  invalid: "save" },
        { label: this.$t('common.cancel'),  invalid: "cancel" },
        
      ];
    },
  },
  methods: {
    //保存按钮
    async save(type) {
      const detail = this.$refs.detail.detailData;
      if(detail.deliveryStatus != 14401){
        return this.$message.error(this.$t('home1.deliveryCantNotUpdate'))
      }
      const detailValidate = await this.$refs.detail.validate();
      const detailListValidate = await this.$refs.detailList.validate();
      if(detailValidate&&detailListValidate){
        let detailInfo = null;
        if(this.$refs.detailList.activeName != 'list'){
          detailInfo = [this.$refs.detailList.detailData];
        }
        let params = {...detail,details:detailInfo};
        //有id为修改，否则为新增
        if(this.updateKey){
          params.id = this.updateKey;
          this.$post("/dcDeliveryOrder/updateDcDeliveryOrder",params).then(res => {
              this.$message.success(this.$t('app.upMes'))
              if(type == 2){
                this.$refs.detailList.activeName = 'list';
              }else{
                this.$router.push({name:"dispatchList"})
              }
            
          })
        }else{
          this.$post("/dcDeliveryOrder/insertDcDeliveryOrder",params).then(res => {
              this.$message.success(this.$t('app.addMes'))
              if(type == 2){
                this.apiParams = {deliveryId:res.data};
                this.updateKey = res.data;
                this.$refs.detailList.activeName = 'list';
              }else{
                this.$router.push({name:"dispatchList"})
              }
            
          })
        }
      }
    },
    //弹框取消
    cancel() {
      this.$router.push({name:"dispatchList"})
    },
  },
  mounted(){
    if(this.$route.query?.id){
      this.updateKey = this.$route.query.id;
      this.apiParams = {deliveryId:this.$route.query?.id}
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
  },
};
</script>

<style scoped></style>
